﻿Public Class frmLeaveBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManager As New PayrollManager
    Private IsType As String
    Private EmpData As Employee
    Private isSickLeave As String
    Sub New(ByVal leaveType As String, ByVal emp As Employee, ByVal type As String)

        ' This call is required by the designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.
        If leaveType = "sick" Then
            isSickLeave = "sick"
        ElseIf leaveType = "vacation" Then
            isSickLeave = "vacation"
        Else
            isSickLeave = ""
        End If
        IsType = type
        EmpData = emp
    End Sub


    Private Sub frmLeaveBR_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Try

            Dim leaveList As List(Of Leave)
            If isSickLeave = "sick" Then
                leaveList = payrollManager.getSickLeaveByEmpId(EmpData.id)
            ElseIf isSickLeave = "vacation" Then
                leaveList = payrollManager.getVacationLeaveByEmpId(EmpData.id)
            Else
                leaveList = payrollManager.getAllLeave()
            End If
            'Dim leaveList As List(Of Leave) = If((isSickLeave), payrollManager.getSickLeaveByEmpId(EmpData.id), payrollManager.getVacationLeaveByEmpId(EmpData.id))
            For Each leave As Leave In leaveList

                Dim item As ListViewItem
                Dim empInfo As EmployeeInfo = If((EmpData.role = "1" And IsType = "1"), New EmployeeInfoManager().retrieveEmployeeInfoByEmployeeId(leave.employee_id), New EmployeeInfoManager().retrieveEmployeeInfoByEmployeeId(EmpData.id))

                Dim lastName As String = empInfo.last_name
                Dim firstName As String = empInfo.first_name
                If Not lastName Is Nothing Then
                    If Not firstName Is Nothing Then
                        item = lvwLeave.Items.Add(empInfo.last_name.ToString() + "," + empInfo.first_name.ToString(), "")
                    Else
                        item = lvwLeave.Items.Add("")
                    End If

                Else
                    item = lvwLeave.Items.Add("")
                End If


                item.SubItems.Add(If((leave.leave_type_id = "0"), "Vacation Leave", "Sick Leave"))
                item.SubItems.Add(leave.leave_from)
                item.SubItems.Add(leave.leave_to)
                item.SubItems.Add(If((leave.status = "0"), "Unapproved", "Approved"))
                item.SubItems.Add(leave.reason)
                item.SubItems.Add(leave.id)
            Next
            grpLeave.Text = "Total Record(s): " + lvwLeave.Items.Count.ToString()
            validator.resizes(lvwLeave)
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        frmLeaveDE.chkAproveLeave.Visible = False
        frmLeaveDE.ShowDialog()
        frmLeaveBR_Load(vbNull, EventArgs.Empty)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpLeave)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnApprove.Enabled = False
        grpLeave.Text = "Total Record(s): 0"
        validator.resizes(lvwLeave)
        Clipboard.Clear()
        btnApprove.Visible = (EmpData.role = "1" And IsType = "1")

        Return 0
    End Function

    Private Sub lvwLeave_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvwLeave.SelectedIndexChanged
        btnEdit.Enabled = (lvwLeave.SelectedItems.Count = 1)
        btnApprove.Enabled = (lvwLeave.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwLeave.SelectedItems.Count > 0)
    End Sub

    Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
        Dim val As String = lvwLeave.SelectedItems(0).SubItems(4).Text
        If val = "Unapproved" Then
            Clipboard.SetText(lvwLeave.SelectedItems(0).SubItems(6).Text)
            frmLeaveDE.chkAproveLeave.Visible = False
            frmLeaveDE.ShowDialog()
            frmLeaveBR_Load(vbNull, EventArgs.Empty)
        Else
            MsgBox("The leave request was already approve.")
        End If
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwLeave.SelectedItems.Count
                payrollManager.deleteItemLeave(lvwLeave.SelectedItems(i - 1).SubItems(6).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmLeaveBR_Load(vbNull, EventArgs.Empty)
            grpLeave.Text = "Total Record(s): " + lvwLeave.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnApprove_Click(sender As System.Object, e As System.EventArgs) Handles btnApprove.Click
        Clipboard.SetText(lvwLeave.SelectedItems(0).SubItems(6).Text)
        frmLeaveDE.chkAproveLeave.Visible = True
        frmLeaveDE.ShowDialog()
        frmLeaveBR_Load(vbNull, EventArgs.Empty)
    End Sub
End Class